package com.pocket.demo.core.mq.rabbitMQ.demo;

import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitMessagingTemplate;
import org.springframework.scheduling.annotation.Scheduled;

import java.util.UUID;

/**
 * @author cookie.
 **/
public class TopicPublisher {
    private static final Logger LOGGER = LoggerFactory.getLogger(TopicPublisher.class);

    public static final String ROUTING_KEY_ERROR = "user.error";
    public static final String ROUTING_KEY_WARN = "role.warn";

    @Resource
    private RabbitMessagingTemplate rabbitMessagingTemplate;

    @Scheduled(cron = "*/5 * * * * *")
    public void sendErrorMessage()
    {
        String message = UUID.randomUUID().toString();
        LOGGER.debug("RabbitMQ : send message to {} : {}", TopicExchangeConfig.TOPIC_EXCHANGE, message);
        rabbitMessagingTemplate.convertAndSend(TopicExchangeConfig.TOPIC_EXCHANGE, ROUTING_KEY_ERROR, ROUTING_KEY_ERROR + message);
    }

    @Scheduled(cron = "*/10 * * * * *")
    public void sendWarnMessage()
    {
        String message = UUID.randomUUID().toString();
        LOGGER.debug("RabbitMQ : send message to {} : {}", TopicExchangeConfig.TOPIC_EXCHANGE, message);
        rabbitMessagingTemplate.convertAndSend(TopicExchangeConfig.TOPIC_EXCHANGE, ROUTING_KEY_WARN, ROUTING_KEY_WARN + message);
    }
}
